import Vue from 'vue'

import core from 'gapi/core'

import App from './App'

import cache from 'gapi/cache.js'

import clientUi from 'gapi/clientUi.js'

import location from 'gapi/location.js'

import commonPage from 'clientApi/commonPage.js'

import * as util from 'js/util.js'

import widget from 'gapi/widget'

new core('dmsaas').start(() => {

	new Vue({
		el: 'body',
		data:{
            selectId:'',
            positionResult:{},
            addressList:[],
            map:{},
            key:['商场','超市','地铁站','写字楼','学校'],
            position:[
                        // {
                        //     "name":"\u516b\u7ef4\u5b66\u6821\u5317\u95e8",
                        //     "address":'sidfjskdfsjfsl',
                        //     "coordinate":"116.399957,39.91095"
                        // },
                        // {
                        //     "name":"\u516b\u7ef4\u5b66\u6821\u5357\u95e8",
                        //     "address":'sjkdfksjdlkslsad',
                        //     "coordinate":"116.399957,39.91095"
                        // }
                    ]
		},
        components: {
            App
        },
		methods:{
			init(){
                var t = this
                // alert(location.requestLocation)
                // cache.getMemoryCache('position', (data) => {
                //     if(data){
                //         this.position = data
                //         this.position.forEach((item)=>{
                //             this.switchView(item)
                //         })
                //     }
                // })
                //定位 todo
                location.requestPOI(function(data){
                    // 搜索当前位置附近的商场等
                    let lon = 116.404
                    let lat = 39.915
                    if(data&&data.lon&&data.lat){
                        lon = data.lon
                        lat = data.lat
                    }else{
                        widget.toast(data.error_msg)
                    }
                    t.map = new BMap.Map("l-map")
                    t.searchAddress(lon,lat)
                })
			},
			//切换地图
			switchView(address){
                let img = util.KqMapImg.attendImgY;
                let myIcon = new BMap.Icon(img, new BMap.Size(24, 32),{
                    imageSize:new BMap.Size(25,31)
                })

				this.positionResult = address
                var point = address.coordinate
				this.map.panTo(new BMap.Point(point.lng,point.lat))
				let marker = new BMap.Marker(point,{icon: myIcon})
				this.map.clearOverlays();
				this.map.addOverlay(marker)
			},
            //查询附近位置
			searchAddress(x,y){
				var t = this
                var mypoint = new BMap.Point(x, y)
                t.map.centerAndZoom(mypoint, 12)
                var options = {
                    // renderOptions:{map:t.map},
                    onSearchComplete: function(results){
                        // 判断状态是否正确
                        if (local.getStatus() == BMAP_STATUS_SUCCESS){
                            for (var i = 0;i< results.length; i ++) {
                                let list = []
                                for (var j = 0; j < results[i].getCurrentNumPois(); j ++){
                                    let s = {}
                                    s.id = ""+i+j
                                    s.name = results[i].getPoi(j).title
                                    s.address = results[i].getPoi(j).address
                                    s.coordinate = results[i].getPoi(j).point
                                    //todo 筛选点经纬度重复的地址
                                    list.push(s)
                                }
                                if(list.length){
                                    t.addressList.push(list)
                                }
                            }
                            t.switchView(t.addressList[0][0]) //设置默认选中地点
                            t.selectId = t.addressList[0][0].id
                        }
                    }
                }
                var local = new BMap.LocalSearch(t.map, options)
                local.searchNearby(t.key,mypoint,1000)
			},
            // 确认按钮
            submit(){
                let position = {}
                position.name = this.positionResult.name
                position.address = this.positionResult.address
                position.coordinate = this.positionResult.coordinate.lng+","+this.positionResult.coordinate.lat
                commonPage.goBack(1,{'key':'position','value':position})
            },
            searchPosition(){
                 window.location.href="dmsaas://KaoqinPositionSearch" 
            },
            updateDate(data){
                let key = data.key
                let value = data.value
                if(key == 'kqPositionSearchResult'){
                    let list = []
                    this.addressList = []
                    value.id="searchResult"
                    list.push(value)
                    this.addressList.push(list)
                    this.searchAddress(value.coordinate.lng,value.coordinate.lat)
                }
            }
		},
		ready(){
            this.init()
            window.loadData = this.updateDate

             /**
             * 设置 添加确认按钮
             * @type {String}
             */
            commonPage.updateTitleBar({
                backAction: '',
                leftDisplay: false,
                leftText: '',
                leftAction: '',
                rightDisplay: true,
                rightText: '确认',
                rightAction: 'doSubmit'
            })

            window.doSubmit = this.submit
		}
	})

})